<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./styles/reset.css">
  <link rel="stylesheet" href="./styles/index.css">
  <script src="./javascript/axios.js"></script>
  <script src="./javascript/flexible.js"></script>
  <link rel="stylesheet" href="./styles/swiper.css">
  <script src="./javascript/swiper.js"> </script>
  <script src="./javascript/art-template.js"></script>
  <script src="./javascript/api/index.js"></script>
  <style>
    li {
      overflow: hidden;
    }

    li img {
      float: left;
      /* 让这个元素不成为事件对象的target */
      pointer-events: none;
    }

    li span {
      float: right;
      /* 让这个元素不成为事件对象的target */
      pointer-events: none;
    }

    ul li>a {
      width: 100%;
      float: left;
    }
  </style>
</head>

<div>
  <!--
    技术分析
    0. 重置浏览器默认样式
    1. 移动端适配 flexible + rem
    2. swiper / slick 轮播图
    3. 生产模拟数据 json-server
    4. 获取数据axios
    5. 渲染数据模版引擎 art-template
    6. iconfont 字体图标
  -->
  <div class="swiper">
    <div class="swiper-wrapper">

    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>
  </div>

  <ul class="hot-sale-box">

  </ul>


  <script id="tpl-user" type="text/html">
    <div class="swiper-slide">
      {{each data}}
        <div class="item">{{$value.title}}</div>
      {{/each}}
    </div>
  </script>

  <script id="hot-sale" type="text/html">

      {{each data}}
        <li data-id="{{$value.id}}">
          <img src="http:{{$value.img}}" alt="">
          <span>{{$value.title}}</span>
        </li>
      {{/each}}

  </script>
  <script>


    async function renderSwiper() {
      let res = await getSwipers()

      let html1 = template('tpl-user', { data: res.data.slice(0, 10) });

      let html2 = template('tpl-user', { data: res.data.slice(10) });

      let container = document.querySelector('.swiper-wrapper')
      container.innerHTML = html1 + html2
      // 必须把页面结构准备好了，才能来初始化轮播图
      let mySwiper = new Swiper('.swiper', {
        loop: true, // 循环模式选项

        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
        }
      })
    }
    renderSwiper()

    async function renderHotSale() {
      let res = await getHotSale()

      let html = template('hot-sale', { data: res.data })

      let hotSaleBox = document.querySelector('.hot-sale-box')
      hotSaleBox.innerHTML = html
      hotSaleBox.addEventListener('click', function (e) {
        let id = e.target.getAttribute('data-id')
        location.assign('./detail.html?id='+id)
      })

    }
    renderHotSale()

  </script>
  </body>

</html>